using System;
using System.Collections.Generic;

namespace NExt.Core
{
    public class AnonymousObject
    {
        public readonly IDictionary<Type, AnonymousObject> Dictionary;

        protected AnonymousObject()
        {
            Dictionary = new Dictionary<Type, AnonymousObject>();
            if (this.GetType() != typeof(AnonymousObject))
                Dictionary.Add(this.GetType(), this);
        }

        public static AnonymousObject operator +(AnonymousObject anonymousObjectObject1, AnonymousObject anonymousObjectObject2)
        {
            AnonymousObject resultAnonymousObject = new AnonymousObject();

            foreach (KeyValuePair<Type, AnonymousObject> keyValuePair in anonymousObjectObject1.Dictionary)
            {
                Type type = keyValuePair.Key;
                AnonymousObject anonymousObject = keyValuePair.Value;
                resultAnonymousObject.Dictionary[type] = anonymousObject;
            }

            foreach (KeyValuePair<Type, AnonymousObject> keyValuePair in anonymousObjectObject2.Dictionary)
            {
                Type type = keyValuePair.Key;
                AnonymousObject anonymousObject = keyValuePair.Value;
                resultAnonymousObject.Dictionary[type] = anonymousObject;
            }

            return resultAnonymousObject;
        }

        public static AnonymousObject operator -(AnonymousObject anonymousObjectObject1, AnonymousObject anonymousObjectObject2)
        {
            AnonymousObject compositeAnonymousObject = new AnonymousObject();

            foreach (KeyValuePair<Type, AnonymousObject> keyValuePair in anonymousObjectObject1.Dictionary)
            {
                Type type = keyValuePair.Key;
                AnonymousObject anonymousObject = keyValuePair.Value;
                compositeAnonymousObject.Dictionary[type] = anonymousObject;
            }

            foreach (KeyValuePair<Type, AnonymousObject> keyValuePair in anonymousObjectObject2.Dictionary)
            {
                Type type = keyValuePair.Key;
                compositeAnonymousObject.Dictionary.Remove(type);
            }
            return compositeAnonymousObject;
        }
    }
}